﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace _110607
{
    public partial class Form2 : Form
    {

        private SqlConnection conn;
   
        public Form2(SqlConnection conn)
        {
            this.conn = conn;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = 0;
            button1.Enabled = false;
            richTextBox1.Text = richTextBox1.Text.Replace("\n", "");
            string sql = "select id,f_CWRQ from foa_wj where id >176968";//76968
            //sql += " where id in(" + richTextBox1.Text + ")";
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                label1.Text = ds.Tables[0].Rows[i]["f_CWRQ"].ToString();
                //if (ds.Tables[0].Rows[i]["id"].ToString() == "186108")
                //{ 
                    
                //}
                Application.DoEvents();
                string s = isMatch2(ds.Tables[0].Rows[i]["f_CWRQ"].ToString());
                if (s != null && s.Length > 0)
                {
                    if (MessageBox.Show(this, "OLD:     " + ds.Tables[0].Rows[i]["f_CWRQ"].ToString() + "NEW:    " + s, "请确认：", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        string sql2 = "update foa_wj set f_CWRQ ='" + s + "'" + " where id = " + ds.Tables[0].Rows[i]["id"];
                        SqlCommand cmd2 = new SqlCommand(sql2, conn);
                        int a = cmd2.ExecuteNonQuery();
                        count += a;
                        if (a > 0) label2.Text = "SUCCESS.";
                        else label2.Text = "ERR.";
                        //MessageBox.Show(sql2);
                    }
                }
                label3.Text = (float)i / (float)ds.Tables[0].Rows.Count * 100 + "%";
            }
            label4.Text = "共计:"+count;
        }
        private string isMatch(string s1)
        {
            string ret = "";
            //Regex reg = new Regex(@"\d{4}-\d{1}");  //2010-3
            //return reg.IsMatch("2010-3"); 
            if (s1.EndsWith("-"))
            {
                ret = s1.Substring(0, s1.Length - 1);
            }
            return ret;
        }
        private string isMatch2(string s1)
        {
            //05 01 2010 12：00AM
            //27-02-2011
            if (string.IsNullOrEmpty(s1)) return null;
            string ret = "";
            if ((s1.Split(' ').Length == 4 || s1.Split('-').Length == 4) && (s1.EndsWith("：00AM") || s1.EndsWith("：AM")))
            {
                if (s1.IndexOf("-") > -1)
                {
                    if (s1.Split('-')[0].Length == 4)
                    {
                        ret = s1.Split('-')[0] + "-" + s1.Split('-')[1] + "-" + s1.Split('-')[2];
                    }
                    else
                    {
                        ret = s1.Split('-')[2] + "-" + s1.Split('-')[1] + "-" + s1.Split('-')[0];
                    }
                }
                else
                {
                    ret = s1.Split(' ')[2] + "-" + s1.Split(' ')[0] + "-" + s1.Split(' ')[1];
                }
            }
            if (s1.Split('-').Length >= 3 && s1.Split('-')[2].Length == 4 && s1.Length <= 10)
            {
                ret = s1.Split('-')[2] + "-" + s1.Split('-')[1] + "-" + s1.Split('-')[0];
            }
            if (s1.IndexOf("--") > -1 && s1.Length == 10)
            {
                ret = s1.Substring(0, 4);
            }

            if (s1.IndexOf("--") > -1)
            {
                ret = s1.Replace("--", "-");
            }

            if (s1.EndsWith("."))
            {
                ret = s1.TrimEnd('.');
            }
            if (s1.IndexOf("T00:00:00-0000000+08:00") > -1)
            {
                ret = s1.Replace("T00:00:00-0000000+08:00", "");
            }
            if (s1.StartsWith(" "))
            {
                s1 = s1.Trim();
                
            }
            
            return ret;
        }

    }
}
